#include "../lvgl/lvgl.h"
#include <stdio.h>
#include "ui_game.h"

// 音乐播放状态
static uint32_t max_progress = 225;
static uint32_t current_progress = 0;
static bool is_playing = false;

// 全局变量
static lv_obj_t * play_btn_icon = NULL;
static lv_timer_t * progress_timer = NULL;
static lv_obj_t * progress_bar = NULL;

/* 按钮按下特效 */
static void btn_press_effect(lv_obj_t * btn, bool press) {
    lv_color_t cyan_color = lv_color_hex(0x00B7EB);
    lv_color_t dark_cyan = lv_color_hex(0x0088AA);
    
    if (press) {
        lv_obj_set_style_bg_color(btn, dark_cyan, 0);
        lv_obj_set_style_bg_opa(btn, LV_OPA_COVER, 0);
        lv_obj_set_style_shadow_color(btn, lv_color_hex(0x00FFFF), 0);
        lv_obj_set_style_shadow_width(btn, 15, 0);
        lv_obj_set_style_shadow_opa(btn, LV_OPA_50, 0);
    } else {
        lv_obj_set_style_bg_color(btn, cyan_color, 0);
        lv_obj_set_style_bg_opa(btn, LV_OPA_70, 0);
        lv_obj_set_style_shadow_width(btn, 0, 0);
    }
}

/* 播放/暂停按钮回调 */
static void play_btn_event_cb(lv_event_t * e) {
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * btn = lv_event_get_target(e);
    
    switch(code) {
        case LV_EVENT_PRESSED:
            printf("播放按钮按下\n");
            btn_press_effect(btn, true);
            break;
            
        case LV_EVENT_RELEASED:
            printf("播放按钮释放\n");
            btn_press_effect(btn, false);
            
            is_playing = !is_playing;
            printf("播放状态: %s\n", is_playing ? "播放" : "暂停");
            
            if(is_playing) {
                lv_label_set_text(play_btn_icon, LV_SYMBOL_PAUSE);
            } else {
                lv_label_set_text(play_btn_icon, LV_SYMBOL_PLAY);
            }
            break;
            
        case LV_EVENT_PRESS_LOST:
            btn_press_effect(btn, false);
            break;
    }
}

/* 进度条更新函数 */
static void progress_timer_cb(lv_timer_t * timer) {
    if (progress_bar && is_playing) {
        // 每秒前进5%
        current_progress += (max_progress * 0.05);
        if(current_progress >= max_progress) {
            current_progress = max_progress;
            is_playing = false;
            if (play_btn_icon) {
                lv_label_set_text(play_btn_icon, LV_SYMBOL_PLAY);
            }
            printf("播放完成，自动暂停\n");
        }
        
        // 更新进度条
        lv_slider_set_value(progress_bar, current_progress, LV_ANIM_ON);
    }
}

/* 创建音乐播放器界面 */
void lv_example_music_player(void) {
    // 清空当前屏幕
    lv_obj_clean(lv_scr_act());
    
    /* 创建主容器 */
    lv_obj_t * player_scr = lv_obj_create(lv_scr_act());
    lv_obj_set_size(player_scr, 800, 480);
    lv_obj_set_style_bg_color(player_scr, lv_color_hex(0x121212), 0);
    lv_obj_set_style_border_width(player_scr, 0, 0);
    lv_obj_set_style_radius(player_scr, 0, 0);
    
    /* 背景图片 */
    lv_obj_t * background = lv_img_create(player_scr);
    lv_img_set_src(background, "S:/IOT/background11.png");
    lv_obj_set_size(background, 800, 480);
    lv_obj_align(background, LV_ALIGN_TOP_LEFT, 0, 0);
    lv_obj_set_style_bg_opa(background, LV_OPA_50, 0);
    lv_obj_move_background(background);
    
    /* 进度条区域 */
    lv_obj_t * progress_cont = lv_obj_create(player_scr);
    lv_obj_set_size(progress_cont, 600, 40);
    lv_obj_align(progress_cont, LV_ALIGN_BOTTOM_MID, 0, -120);
    lv_obj_set_style_bg_opa(progress_cont, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(progress_cont, 0, 0);
    
    // 当前时间
    lv_obj_t * current_time_label = lv_label_create(progress_cont);
    char time_str[8];
    sprintf(time_str, "%d:%02d", (int)(current_progress / 60), (int)(current_progress % 60));
    lv_label_set_text(current_time_label, time_str);
    lv_obj_set_style_text_color(current_time_label, lv_color_hex(0xbbbbbb), 0);
    lv_obj_set_style_text_font(current_time_label, &lv_font_montserrat_16, 0);
    lv_obj_align(current_time_label, LV_ALIGN_LEFT_MID, 0, 0);
    
    // 进度条
    progress_bar = lv_slider_create(progress_cont);
    lv_slider_set_range(progress_bar, 0, max_progress);
    lv_slider_set_value(progress_bar, current_progress, LV_ANIM_OFF);
    lv_obj_set_size(progress_bar, 500, 6);
    lv_obj_align(progress_bar, LV_ALIGN_CENTER, 0, 0);
    
    // 进度条样式
    lv_obj_set_style_bg_color(progress_bar, lv_color_hex(0x444444), LV_PART_MAIN);
    lv_obj_set_style_bg_color(progress_bar, lv_color_hex(0x00B7EB), LV_PART_INDICATOR);
    lv_obj_set_style_bg_color(progress_bar, lv_color_hex(0x00B7EB), LV_PART_KNOB);
    lv_obj_set_style_pad_all(progress_bar, 0, LV_PART_KNOB);
    lv_obj_set_style_radius(progress_bar, 3, LV_PART_MAIN);
    lv_obj_set_style_radius(progress_bar, 3, LV_PART_INDICATOR);
    
    // 总时长
    lv_obj_t * total_time_label = lv_label_create(progress_cont);
    lv_label_set_text(total_time_label, "3:45");
    lv_obj_set_style_text_color(total_time_label, lv_color_hex(0xbbbbbb), 0);
    lv_obj_set_style_text_font(total_time_label, &lv_font_montserrat_16, 0);
    lv_obj_align(total_time_label, LV_ALIGN_RIGHT_MID, 0, 0);
    
    /* 控制按钮区域 */
    lv_obj_t * btn_cont = lv_obj_create(player_scr);
    lv_obj_set_size(btn_cont, 500, 80);
    lv_obj_align(btn_cont, LV_ALIGN_BOTTOM_MID, 0, -40);
    lv_obj_set_style_bg_opa(btn_cont, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(btn_cont, 0, 0);
    
    // 定义青蓝色
    lv_color_t cyan_color = lv_color_hex(0x00B7EB);
    
    // 播放/暂停按钮
    lv_obj_t * play_btn = lv_btn_create(btn_cont);
    lv_obj_set_size(play_btn, 80, 80);
    lv_obj_set_style_radius(play_btn, 40, 0);
    lv_obj_set_style_bg_color(play_btn, cyan_color, 0);
    lv_obj_set_style_bg_opa(play_btn, LV_OPA_70, 0);
    lv_obj_set_style_border_width(play_btn, 0, 0);
    lv_obj_align(play_btn, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_event_cb(play_btn, play_btn_event_cb, LV_EVENT_ALL, NULL);
    
    play_btn_icon = lv_label_create(play_btn);
    lv_label_set_text(play_btn_icon, is_playing ? LV_SYMBOL_PAUSE : LV_SYMBOL_PLAY);
    lv_obj_set_style_text_color(play_btn_icon, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_text_font(play_btn_icon, &lv_font_montserrat_28, 0);
    lv_obj_center(play_btn_icon);
    
    /* 启动进度更新定时器 */
    progress_timer = lv_timer_create(progress_timer_cb, 1000, NULL);
    lv_timer_set_repeat_count(progress_timer, -1);
    button_back(); // 返回按钮
    printf("简单音乐播放器界面创建完成\n");
}